package com.util;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SiteUtils {

    /**
     * @Desc: 按通道（20个一通道）进行分组
     **/
    public static Map<Integer, List<String>> groupByAisle(List<String> siteTemp){
        Map<Integer, List<String>> reMap = new HashMap<>();
        for (int index = 0; index < siteTemp.size(); index++) {
            Integer currentAisles = Integer.parseInt(String.format("%.0f", Math.floor(index / 20) + 1));

            if (ObjectUtils.isEmpty(reMap.get(currentAisles))) {
                reMap.put(currentAisles, new ArrayList<>());
            }
            reMap.get(currentAisles).add(siteTemp.get(index));
        }
        return reMap;
    }

    /**
     * @Desc: 过滤分组获取电池仓通道测温点数据
     **/
    public static Map<Integer, List<String>> filterWarehouse(List<String> dataList, Integer warehousePoint) {
        //原数据按通道进行分组
        Map<Integer, List<String>> siteAisleTemp = SiteUtils.groupByAisle(dataList.subList(2, dataList.size()));
        Map<Integer, List<String>> reMap = new HashMap<>();
        for (int aisleNo = 1; aisleNo <= warehousePoint; aisleNo++) {
            if (!ObjectUtils.isEmpty(siteAisleTemp.get(aisleNo))) {
                reMap.put(aisleNo, siteAisleTemp.get(aisleNo));
            }
        }
        return reMap;
    }

    /**
     * @Desc: 获取最大值
     **/
    public static BigDecimal getMax(List<String> temps){
        List<String> collect = temps.stream().filter(s -> !StringUtils.isEmpty(s)).collect(Collectors.toList());
        if(ObjectUtils.isEmpty(collect)||collect.size()<1){
            return new BigDecimal("0.0");
        }
        return collect.stream().map(BigDecimal::new).max(BigDecimal::compareTo).get();
    }

}
